<?php

namespace App\Kuafu\Shop\Validate;

use App\Kuafu\Banner\Model\BannerCommonModel;
use App\Kuafu\Base\Enum\DBEnum;
use App\Kuafu\Base\Model\BaseModel;
use App\Kuafu\Base\Validate\BaseValidate;
use App\Kuafu\Pay\Model\PayStreamModel;
use App\Kuafu\ProductMenu\Model\ProductMenu;
use App\Kuafu\Shop\Enum\AreaCodeEnum;
use App\Kuafu\Shop\Enum\CodeEnum;
use App\Kuafu\Shop\Model\AreaModel;
use App\Kuafu\Shop\Model\Shop;
use App\Kuafu\User\Enum\UserCodeEnum;
use App\Kuafu\User\Exceptions\UserException;
use App\Kuafu\Shop\Model\ShopPaymentsModel;
use Illuminate\Http\Request;
use Illuminate\Validation\Rule;

class ShopInvoiceValidate extends BaseValidate
{
    public static array $attributes = [
        'invoiceNo'    => '纳税识别人号',
        'kpjh'         => '开票机号',
        'fee'          => '适用税率',
        'name'         => '公司名称',
        'address'      => '联系地址',
        'tel'          => '联系电话',
        'contact'      => '联系人',
        'isDiscount'   => '是否优惠',
        'discountName' => '优惠政策类型',
    ];

    /**
     * 获取规则信息
     *
     * @param array $fields
     * @return string[]
     */
    public static function getRules(array $fields = []): array
    {
        return self::getActuallyRules([
            'invoiceNo'    => 'required|bail',
            'kpjh'         => 'required|bail',
            'fee'          => 'required|bail|numeric|between:1,100',
            'name'         => 'required|bail',
            'tel'          => '',
            'address'      => '',
            'contact'      => '',
            'isDiscount'   => 'required|integer|between:0,1',
            'discountName' => 'exclude_unless:isDiscount,1|required',
        ], $fields);
    }

    public static function shopInvoiceEditValidate($params): array
    {
        if ($params['selected'] == CodeEnum::SHOP_INVOICE_CODE_1) {
            return self::shopInvoiceMoreDoValidate($params[CodeEnum::SHOP_INVOICE_CODE_1]);
        } else {
            return self::shopInvoicNoValidate();
        }
    }


    /**
     * 关闭门店打印
     *
     * @param $params
     * @return array
     */
    public static function shopInvoicNoValidate(): array
    {
        return [
            '',
            [
                'selected'                    => CodeEnum::SHOP_INVOICE_CODE_0,
                CodeEnum::SHOP_INVOICE_CODE_0 => []
            ]
        ];

    }

    /**
     * 盟度 门店开票信息校验方式
     *
     * @param $params
     * @return array
     */
    public static function shopInvoiceMoreDoValidate($params): array
    {

        return self::baseValidate(
            $params,
            ['invoiceNo', 'kpjh', 'fee', 'name', 'address', 'tel', 'contact', 'isDiscount', 'discountName'],
            [],
            function (array $params) {
                $paramsObj = (object)$params;
                return [
                    'selected'                    => CodeEnum::SHOP_INVOICE_CODE_1,
                    CodeEnum::SHOP_INVOICE_CODE_1 => [
                        'invoiceNo'    => $paramsObj->invoiceNo,
                        'kpjh'         => $paramsObj->kpjh,
                        'fee'          => $paramsObj->fee,
                        'isDiscount'   => $paramsObj->isDiscount,
                        'discountName' => $paramsObj->discountName,
                        'address'      => $paramsObj->address,
                        'name'         => $paramsObj->name,
                        'tel'          => $paramsObj->tel,
                        'contact'      => $paramsObj->contact,
                    ]
                ];
            }
        );
    }

}
